﻿namespace GuiaTest
{
    using System;
    using System.Threading;
    using System.Windows.Automation;
    using Guia.Core;
    using Guia.Elements;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using WpfTestApplication;

    [TestClass]
    public class WindowTest : AbstractWindowTest
    {

        protected override Type TestWindowType()
        {
            return typeof(WindowTestWindow);
        }

        protected override string TestWindowName()
        {
            return "WindowTestWindow";
        }

        [TestMethod, ExpectedException(typeof(OperationNotSupportedException))]
        public void TestMaximizeNotAllowed()
        {
            WindowControl window2 = WindowControl.Start<WindowTestWindow2>();
            try
            {
                window2.Maximize();
            }
            finally
            {
                window2.Stop();
            }
        }

        [TestMethod]
        public void TestMaximizedWindow()
        {
            Assert.IsNotNull(window);
            window.Maximize();
            Assert.AreEqual(WindowVisualState.Maximized, window.GetVisualState());
        }

        [TestMethod]
        public void TestRecoverMaximized()
        {
            Assert.IsNotNull(window);
            window.Maximize();
            Assert.AreEqual(WindowVisualState.Maximized, window.GetVisualState());
            // now, recover to normal
            window.RecoverNormal();
            Assert.AreEqual(WindowVisualState.Normal, window.GetVisualState());
        }

        [TestMethod]
        public void TestClose()
        {
            Assert.IsNotNull(window);
            window.Close();
            Delay.ForMilliseconds(100);
            // Depending on executing-method, other exceptions are thrown.
            // This is probably caused by the different dispatcher handlings. 
            // A Close-event of an application forces the dispatcher to shutdown. 
            // In WindowControl-mode, the dispatcher-shutdown is made in the 
            // test-teardown, thus the patterns are still accessable, hence
            // the exception occures later.
            try
            {
                window.GetVisualState();
                Assert.Fail("Window still open");
            } catch (OperationNotSupportedException)
            {
                // Application
                Assert.IsTrue(true, "Window was closed");
            } catch (ElementNotAvailableException)
            {
                // WindowControl
                Assert.IsTrue(true, "Window was closed");
            }
        }

        [TestMethod]
        public void TestMinimize()
        {
            Assert.IsNotNull(window);
            window.Minimize();
            Assert.AreEqual(WindowVisualState.Minimized, window.GetVisualState());
        }

        [TestMethod, ExpectedException(typeof(OperationNotSupportedException))]
        public void TestMinimizeNotAllowed()
        {
            WindowControl window2 = WindowControl.Start<WindowTestWindow2>();
            try
            {
                window2.Minimize();
            }
            finally
            {
                window2.Stop();
            }
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestGetMessageBoxWithNullValue()
        {
            this.window.GetMessageBox(null);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestGetMenuWithNullValue()
        {
            this.window.GetMenu(null);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestGetSaveFileDialogWithNullValue()
        {
            this.window.GetSaveFileDialog(null);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void TestGetOpenFileDialogWithNullValue()
        {
            this.window.GetOpenFileDialog(null);
        }
    }
}
